﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using CE.Provider;
using CE.Global;

public partial class Provider_CarInsurance_ProviderCarOccupation : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCIOCP";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!Page.IsPostBack)
        {
            usrEffectivityDate.StartYear = CE.Global.General.GetUTCDateTime().Year;
            usrEffectivityDate.EndYear = CE.Global.General.GetUTCDateTime().Year + 2;

            DataTable dtOccupation = General.GetComboItem("Occupation", false);
            ddlCXOccupation.DataSource = dtOccupation;
            ddlCXOccupation.DataTextField = dtOccupation.Columns[0].ColumnName.ToString();
            ddlCXOccupation.DataValueField = dtOccupation.Columns[1].ColumnName.ToString();
            ddlCXOccupation.DataBind();
            ddlCXOccupation.Items.Insert(0, new ListItem("Select", "-1"));
            dtOccupation = null;

            if (Request.Params["CXOccupationId"] != null)
            {
                int _cxoccupationid = 0;
                if (int.TryParse(Request.Params["CXOccupationId"].ToString(), out _cxoccupationid))
                {
                    ddlCXOccupation.Text = _cxoccupationid.ToString();
                    ddlCXOccupation_SelectedIndexChanged(this, null);
                    //ShowProviderOccupation(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), _cxoccupationid);
                }
            }
        }
    }

    protected void ibtSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);

            if (CheckAuthCode(_authcode, 100))
            {
                CarOccupation _caroccupation = new CarOccupation();
                CarOccupationDB _caroccupationdb = new CarOccupationDB();

                _caroccupation.ProviderOccupationId = CheckProviderOccupation(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToInt32(ddlCXOccupation.Text));

                int _trantype = 1;

                if (_caroccupation.ProviderOccupationId > 0)
                {
                    _trantype = 2;
                }
                _caroccupation.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
                _caroccupation.ProviderCompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());

                _caroccupation.Description = txtYourOccupation.Text;
                _caroccupation.OccupationRate = Convert.ToSingle(txtOccupationRate.Text);
                _caroccupation.CXOccupationId = Convert.ToInt32(ddlCXOccupation.Text);
                _caroccupation.Code = Convert.ToInt32(txtCode.Text);
                _caroccupation.Status = chkStatus.Checked;

                if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
                {
                    _caroccupation.FInfo1 = float.Parse(txtNewRate.Text);
                    _caroccupation.DInfo1 = usrEffectivityDate.GetDate();

                }
                else
                {
                    _caroccupation.FInfo1 = float.Parse(txtOccupationRate.Text);
                    _caroccupation.DInfo1 = CE.Global.General.GetUTCDateTime();

                }

                _caroccupation.ProviderOccupationId = _caroccupationdb.Insert(_caroccupation);
                if (_caroccupation.ProviderOccupationId > 0)
                {
                    InsertProviderAuthTransaction(_caroccupation.ProviderOccupationId, "CIOccupation", _trantype, 100, _authcode, _caroccupation.ProviderCompanyId, _caroccupation.RefCEUserId);
                    ProviderUpdateSave(_caroccupation.ProviderOccupationId);
                    lblMessage.Text = "Updated successfully.";
                    Response.Redirect("ViewProviderCarOccupation.aspx");
                }
                else
                {
                    lblMessage.Text = "Updating failed.";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }
    
    private void ProviderUpdateSave(Int64 ProviderRefId)
    {
        ProviderUpdates _providerupdates = new ProviderUpdates();
        ProviderUpdatesDB _providerupdatesDB = new ProviderUpdatesDB();

        _providerupdates.UpdateId = 0;
        _providerupdates.CXTableId = GetCXTableId();
        _providerupdates.ProviderRefId = ProviderRefId;

        if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
        {
            //Scheduled
            _providerupdates.UpdateType = 1;
        }
        else
        {
            //Immediate
            _providerupdates.UpdateType = 0;
        }

        _providerupdates.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]);
        _providerupdates.RefCEUserID = Convert.ToInt64(Session[General.UserId].ToString());

        _providerupdatesDB.Insert(_providerupdates);

    }

    private Int32 GetCXTableId()
    {
        Int32 CXID = 0;

        ProviderTableListings _tablelist = new ProviderTableListings();
        ProviderTableListingsDB _tablelistdb = new ProviderTableListingsDB();

        int ProviderType = 100; //Car Insurance
        string CXTable = "tblProviderCarOccupation";

        _tablelist = _tablelistdb.GetProviderTableListingsSingleItem(ProviderType, Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), CXTable);

        if (_tablelist != null)
            CXID = _tablelist.CXTableId;

        return CXID;

    }

    protected void ShowProviderOccupation(Int64 ProviderCompanyId, Int32 CXOccupationId)
    {
        CarOccupation _caroccupation = new CarOccupation();
        CarOccupationDB _caroccupationdb = new CarOccupationDB();

        _caroccupation = _caroccupationdb.GetProviderOccupation(ProviderCompanyId, CXOccupationId);
        if (_caroccupation != null)
        {
            lblProviderOccupationId.Text = _caroccupation.ProviderOccupationId.ToString();
            txtYourOccupation.Text = _caroccupation.Description;
            txtOccupationRate.Text = _caroccupation.OccupationRate.ToString();
            txtCode.Text = _caroccupation.Code.ToString();
            chkStatus.Checked = _caroccupation.Status;

            if (_caroccupation.DInfo1 > CE.Global.General.GetUTCDateTime())
            {
                divNewRate.Visible = true;
                txtNewRate.Text = _caroccupation.FInfo1.ToString();
                rdoEffectivity.SelectedValue = "Yes";
            }
            else
            {
                divNewRate.Visible = false;
                rdoEffectivity.SelectedValue = "No";
            }
            
            usrEffectivityDate.Day = _caroccupation.DInfo1.Day;
            usrEffectivityDate.Month = _caroccupation.DInfo1.Month;
            usrEffectivityDate.Year = _caroccupation.DInfo1.Year;
        }
        else
        {
            ClearFields();
        }
    }

    protected Int64 CheckProviderOccupation(Int64 ProviderCompanyId, Int32 CXOccupationId)
    {
        CarOccupation _caroccupation = new CarOccupation();
        CarOccupationDB _caroccupationdb = new CarOccupationDB();
        Int64 _retValue = 0;
        _caroccupation = _caroccupationdb.GetProviderOccupation(ProviderCompanyId, CXOccupationId);
        if (_caroccupation != null)
        {
            _retValue = _caroccupation.ProviderOccupationId;
        }
        return _retValue;
    }

    protected void ddlCXOccupation_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowProviderOccupation(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToInt32(ddlCXOccupation.Text));
    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ViewProviderCarOccupation.aspx");
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (lblProviderOccupationId.Text != "0" && txtAuthCode.Text.Trim().Length > 0)
        {
            //Int64 _caryearid = CheckCarYear(Convert.ToInt32(ddlFromYear.Text), Convert.ToInt32(ddlToYear.Text), Convert.ToInt32(ddlOccupation.Text));
            Int64 _occupationid = Convert.ToInt64(lblProviderOccupationId.Text);
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            Int64 _providercompanyid = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
            Int64 _refceuserid = Convert.ToInt64(Session[General.UserId].ToString());
            if (CheckAuthCode(_authcode, 100))
            {
                if (_occupationid != 0)
                {
                    CarOccupationDB _caroccupationdb = new CarOccupationDB();
                    if (_caroccupationdb.Delete(_occupationid, _providercompanyid))
                    {
                        InsertProviderAuthTransaction(_occupationid, "CIOccupation", 3, 100, _authcode, _providercompanyid, _refceuserid);                        
                        Response.Redirect("ViewProviderCarOccupation.aspx");
                    }
                    else
                    {
                        lblMessage.Text = "Deleting failed.";
                    }
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    private void ClearFields()
    {
        lblProviderOccupationId.Text = "0";
        //ddlCXOccupation.Text = "-1";
        txtYourOccupation.Text = "";
        txtOccupationRate.Text = "";
        txtCode.Text = "";
        chkStatus.Checked = false;
    }

    public void EffectiveWhen(object sender, EventArgs e)
    {
        if (rdoEffectivity.SelectedValue == "Yes")
        {
            divNewRate.Visible = true;
        }
        else
        {
            divNewRate.Visible = false;
        }

        usrEffectivityDate.Day = CE.Global.General.GetUTCDateTime().Day;
        usrEffectivityDate.Month = CE.Global.General.GetUTCDateTime().Month;
        usrEffectivityDate.Year = CE.Global.General.GetUTCDateTime().Year;
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        ddlCXOccupation.Text = "-1";
        ClearFields();
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
            btnNew.Enabled = false;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            ibtSubmit.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            ibtSubmit.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = true;
        }
    }

}
